package com.ssm.pre.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.ssm.pre.dao.RepairDao;
import com.ssm.pre.dao.UserDao;
import com.ssm.pre.service.RepairService;
import com.ssm.pre.util.DateUtil;
import com.ssm.pre.util.PageCount;
import com.ssm.pre.util.TemplateUtil;
import com.ssm.pre.util.ToolUtil;
import com.ssm.pre.util.UploadUtil;
import com.ssm.util.Constants;
import com.ssm.util.object.InputObject;
import com.ssm.util.object.OutputObject;
/**
 * 以后根据ID查询信息时，要将id写成表名ID，如repairID
 * @author ZHANG
 *
 */
@Service
public class RepairServiceImpl implements RepairService{
	@Autowired
	private RepairDao rDao;
	
	@Override
	public void queryRepairList(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		List<Map<String, Object>> beans = rDao.queryRepairList(map);
		int page = Integer.parseInt(map.get("offset").toString())/Integer.parseInt(map.get("limit").toString());
		page++;
		List<Map<String, Object>> params = PageCount.page(beans, page, Integer.parseInt(map.get("limit").toString()));
		outputObject.setBeans(params);
		outputObject.settotal(beans.size()); 
	}

	@Override
	public void addRepair(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();

		
		Map<String, Object> beans = rDao.queryHouseId(map);
		
		map.put("houseId", beans.get("id"));
		map.put("createTime", ToolUtil.getTimeAndToString());
		int num = rDao.addRepair(map);
		outputObject.settotal(num);
		
	}

	@Override
	public void editRepair(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> params = inputObject.getParams();
		//根据RepairID查询维修工信息
		Map<String, Object> map = rDao.queryServiceManByRepairID(params);
		if (Integer.parseInt((String) params.get("faultState"))==2) {
			params.put("noticeTime", ToolUtil.getTimeAndToString());
			
		}else if (Integer.parseInt((String) params.get("faultState"))==3) {
			params.put("repairTime", ToolUtil.getTimeAndToString());
			
		}
		params.put("serviceName", map.get("sName").toString()+"("+map.get("sPhone").toString()+")");
		rDao.editRepair(params);
		TemplateUtil.Template3(params);
	}

	@Override
	public void deleteRepairById(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> params = inputObject.getParams();
		int num = rDao.deleteRepairById(params);
	
		outputObject.settotal(num);
		
	}

	@Override
	public void findRepairById(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		Map<String, Object> bean = rDao.queryRepairById(map);
		
		outputObject.setBean(bean);
		
		
	}

	@Override
	public void queryRoomNumByOneSelect(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		List<Map<String, Object>> beans = rDao.queryRoomNumByOneSelect(map);
		outputObject.setBeans(beans);
	}

	@Override
	public void addWechatRepair(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		Map<String, Object> params = inputObject.getWechatLogParams();
		map.put("faultState", 1);//维修状态默认为1
		map.put("createId",params.get("id"));//family表中的ID
		map.put("createTime", ToolUtil.getTimeAndToString());
		//订单号 当月时间20171202  + 随机生产的adsdsf 共18位
		map.put("repairNumber", DateUtil.getTimeToString()+DateUtil.getUUID()+DateUtil.getUUID());
		
		rDao.addWechatRepair(map);
		map.put("openId", params.get("openId"));
		map.put("familyName", params.get("familyName"));
		map.put("serviceName", 0);
		map.put("hInfo",params.get("hInfo"));
		//发送模板消息
		TemplateUtil.Template3(map);
	}


	@Override
	public void addWechatImg(InputObject inputObject, OutputObject outputObject, CommonsMultipartFile files) throws Exception {
		Map<String, Object> map = UploadUtil.imgFileUpload(inputObject,
				outputObject, files);
		if (map != null) {
			outputObject.setBean(map);
		} else {
			outputObject.setreturnMessage("文件类型不正确，只允许上传jpg,png,jpeg,svg格式的图片");
		}
		 
		
	}
	/**
	 * 查询infoName下拉列表
	 */
	@Override
	public void queryinfoNameByfaultId(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		List<Map<String, Object>> params = rDao.queryinfoNameByfaultId(map);
		outputObject.setBeans(params);
		
		
	}
	/**
	 * 查询故障类型下拉列表
	 */
	@Override
	public void queryfaultNameList(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		List<Map<String, Object>> params = rDao.queryfaultNameList(map);
		List<Map<String, Object>> faultList = new ArrayList<Map<String, Object>>();
		
		for(Map<String, Object> bean : params){
			if(bean.get("type").toString().equals("1")){
				faultList.add(bean);
			}else{
				faultList.add(bean);
			}
		}
		
		outputObject.setBeans(faultList);
	}
	
	/**
	 * 根据faultName查找维修人
	 * @param inputObject
	 * @param outputObject
	 * @throws Exception
	 */
	@Override
	public void queryserviceManNameByfaultName(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		List<Map<String, Object>> bean = rDao.queryserviceManNameByfaultName();
		outputObject.setBeans(bean);
		
		
	}

	/**
	 * 查询报修记录
	 */
	@Override
	public void queryrepairRecord(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		List<Map<String, Object>> bean = rDao.queryrepairRecord(map);
		
		outputObject.setBeans(bean);
		
	}

}
